package com.project.config;

import com.project.shiro.CustomRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;


@Configuration
public class ShiroConfig {
    /**
     * 注册安全管理器
     * @return 安全管理器对象
     */
    @Bean
    public DefaultWebSecurityManager getSecurityManager(CustomRealm customRealm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(customRealm);
        return manager;
    }

    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean filterFactoryBean(DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        factoryBean.setSecurityManager(manager);
        //认证失败后转发路径
        Map<String,String> filterMap = new LinkedHashMap<>();
        //添加公共资源
        filterMap.put("/user/login","anon");
        filterMap.put("/user/loginOut","anon");
        filterMap.put("/user/updatePwd","anon");
        //添加受限资源
        filterMap.put("/**","authc");
        //设置拦截器链
        factoryBean.setFilterChainDefinitionMap(filterMap);
        return factoryBean;
    }
}
